package com.rongshu.utils.clickableSpan;

import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;

/**
 * Created by weimy on 2017/10/19.
 */

public class ExPandableMovementMethod extends LinkMovementMethod {
    private String tag = ExPandableMovementMethod.class.getSimpleName();
    //onTouchEvent中触摸position坐标存在spanableClickable点击区域
    private SpanableClickable mPressedSpan = null;
    //点击是区域还是全部
    private boolean isAll;

    public boolean isAll() {
        return isAll;
    }

    public void setAll(boolean all) {
        isAll = all;
    }

    //重写onTouchEvent事件
    @Override
    public boolean onTouchEvent(TextView widget, Spannable spannable, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //通过偏移量确定该点击部分是否存在Spannable的添加的SpanableClick点击事件内
                mPressedSpan = getPressedSpan(widget, spannable, event);
                if (mPressedSpan != null) {
                    setAll(false);
                    mPressedSpan.setPressed(true);
                    //设置点击区域的背景色
                    Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan), spannable.getSpanEnd(mPressedSpan));
                } else {
                    setAll(true);
                    widget.setBackgroundColor(widget.getResources().getColor(R.color.color_c9cbcc));
                }
                break;
            case MotionEvent.ACTION_MOVE:
                SpanableClickable touchedSpan = getPressedSpan(widget, spannable, event);
                if (mPressedSpan != null && touchedSpan != mPressedSpan) {
                    mPressedSpan.setPressed(false);
                    spannable.removeSpan(mPressedSpan);
                    mPressedSpan = null;
                    Selection.removeSelection(spannable);
                }
                break;
            default:
                if (mPressedSpan != null) {
                    mPressedSpan.setPressed(false);
                }
                mPressedSpan = null;
                Selection.removeSelection(spannable);
                widget.setBackgroundResource(android.R.color.transparent);
                break;
        }
        return super.onTouchEvent(widget, spannable, event);
    }


    private SpanableClickable getPressedSpan(TextView widget, Spannable spannable, MotionEvent event) {
        //获取触摸的x,y相对坐标(相对于本身widget布局,包含padding在内)
        int x = (int) event.getX();
        int y = (int) event.getY();
        Log.i(tag, "onTouchEvent:x=" + x + "---y=" + y);
        //获取widget整个paddingLeft,paddingTop
        int paddingLeft = widget.getTotalPaddingLeft();
        int paddingTop = widget.getTotalPaddingTop();
        //如果widget内容有ScrollView(相对与widget的坐标将发生改变,需要加上Scroll距离)
        int scrollX = widget.getScrollX();
        int scrollY = widget.getScrollY();
        //当部分文字添加spanClickable,该部分文字的在widget的真正坐标为;
        x = x - paddingLeft + scrollX;
        y = y - paddingTop + scrollY;
        //通过y坐标确定部分文字在widget的第几行
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        //在通过行数，x确定其列的偏移量
        int off = layout.getOffsetForHorizontal(line, x);
        //通过偏移量确定该点击部分是否存在Spannable的添加的SpanableClick点击事件内
        SpanableClickable[] link = spannable.getSpans(off, off, SpanableClickable.class);
        if (link.length > 0) {
            mPressedSpan = link[0];
        }
        return mPressedSpan;
    }

    //事件被拦截，去除背景
    public void cancleBg(View widget) {
        widget.setBackgroundResource(R.color.transparent);
    }
}

